package com.projectx.action.login;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.textfield.TextInputLayout;
import com.projectx.R;
import com.projectx.action.findPassword.FindPassword;
import com.projectx.action.talk.TalkListActivity;
import com.projectx.entity.UserData;
import com.projectx.util.ActivityManage;

import org.litepal.LitePal;

public class LoginActivity2 extends AppCompatActivity {
    ProgressBar progressBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LitePal.initialize(this);
        ActivityManage.addActivity("LoginActivity2", this);
        setContentView(R.layout.activity_login2);

        final EditText passwordInput = findViewById(R.id.PasswordEditText);
        final TextInputLayout passwordInputLayout = findViewById(R.id.PasswordEditField);
        final TextView accountText = findViewById(R.id.AccountText);
        final TextView welcome = findViewById(R.id.WelcomeText);
        final Intent LoginIntent = getIntent();
        final Button next = findViewById(R.id.LoginNext2);
        final Button forgetPassword = findViewById(R.id.ForgetPassword);
        final String accountName = LoginIntent.getStringExtra("accountName");
        String pwdIn = null;
        try { //判断是否传入密码
            pwdIn = LoginIntent.getStringExtra("password");
        } catch (Exception e){
            e.printStackTrace();
        }

        if(pwdIn != null && pwdIn.length() > 0){
            passwordInput.setText(pwdIn);
        }
        progressBar = findViewById(R.id.loginProgress);
        progressBar.setVisibility(View.INVISIBLE);

        TextPaint textPaint = accountText.getPaint();
        textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        textPaint.setStrokeWidth(2);
        accountText.setText(accountName);

        TextPaint textPaint1 = welcome.getPaint();
        textPaint1.setStyle(Paint.Style.FILL_AND_STROKE);
        textPaint1.setStrokeWidth(2);


        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String password = passwordInput.getText().toString();
                if (password.isEmpty()) {
                    passwordInputLayout.setError("请输入非空密码");
                } else {
                    passwordInputLayout.setErrorEnabled(false);
                    progressBar.setVisibility(View.VISIBLE);
                    //利用FutureTask阻断线程
//                    FutureTask<JSONObject> task = new FutureTask<>(new Callable<JSONObject>() {
//                        @Override
//                        public JSONObject call() throws Exception {
//                            JSONObject getMessage = null;
//                            try {
//                                Socket socket = new Socket("106.15.192.117", 2333);
//                                Gson gson = new Gson();
//                                User user = new User();
//                                user.setFlag(1);
//                                user.setUserName(accountName);
//                                user.setPassword(password);
//                                String message = gson.toJson(user);
//                                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
//                                dataOutputStream.writeUTF(message);
//                                dataOutputStream.flush();
//                                DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
//                                String netStr = dataInputStream.readUTF();
//                                try {
//                                    getMessage = new JSONObject(netStr);
//                                    try {
//                                        int code = getMessage.getInt("code");
//                                        progressBar.setVisibility(View.INVISIBLE);
//                                        if (code == -1) {
//                                            runOnUiThread(new Runnable() {
//                                                @Override
//                                                public void run() {
//                                                    passwordInputLayout.setError("用户名密码不匹配");
//                                                }
//                                            });
//                                        } else if (code == 0) {
//                                            Intent intent = new Intent(LoginActivity2.this, TalkListActivity.class);//进入主界面
//                                            intent.putExtra("accountName", accountName);
//                                            intent.putExtra("password",password);
//                                            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//                                            startActivity(intent);
//                                            UserData userData = new UserData();
//                                            userData.setUsername(accountName);
//                                            userData.setPassword(password);
//                                            userData.setLogin(true);
//                                            userData.save();
//                                            ActivityManage.removeAllActivityExcept("TalkListActivity");
//                                        }
//                                    } catch (JSONException je) {
//                                        Toast.makeText(LoginActivity2.this, "服务器错误", Toast.LENGTH_SHORT).show();
//                                        je.printStackTrace();
//                                    } catch (Exception ne){
//                                        ne.printStackTrace();
//                                        Toast.makeText(LoginActivity2.this, "网络请求失败", Toast.LENGTH_SHORT).show();
//                                    } finally {
//                                        progressBar.setVisibility(View.INVISIBLE);
//                                    }
//
//                                } catch (JSONException e) {
//                                    Log.d("kmh", "doInBackground: 后端JSON字符串有问题祭天吧");
//                                } finally {
//                                    dataInputStream.close();
//                                    dataOutputStream.close();
//                                    socket.close();
//                                }
//
//                            } catch (IOException ioe) {
//                                ioe.printStackTrace();
//                            }
//                            return getMessage;
//                        }
//                    });
//                    Thread thread = new Thread(task);
//                    thread.start();
                    //解析验证JSON字符串返回数据
                    Intent intent = new Intent(LoginActivity2.this, TalkListActivity.class);//进入主界面
                    intent.putExtra("accountName", accountName);
                    intent.putExtra("password",password);
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                    UserData userData = new UserData();
                    userData.setUsername(accountName);
                    userData.setPassword(password);
                    userData.setLogin(true);
                    userData.save();
                    ActivityManage.removeAllActivityExcept("TalkListActivity");
                }
            }
        });

        forgetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity2.this, FindPassword.class);
                intent.putExtra("accountName", accountName);
                startActivity(intent);
            }
        });
    }
}